package com.internetCafes.spms.web.customer.controller;

import com.internetCafes.spms.common.base.BaseWebController;
import com.internetCafes.spms.common.module.excel.ExcelUtils;
import com.internetCafes.spms.common.utils.entity.RR;
import com.internetCafes.spms.common.utils.entity.page.PageItemDTO;
import com.internetCafes.spms.common.utils.entity.page.PageItemVO;
import com.internetCafes.spms.core.annotation.SysLog;
import com.internetCafes.spms.web.customer.bizservice.customer.IEntCustomerCooperationBizService;
import com.internetCafes.spms.web.customer.model.ExcelImportResultRes;
import com.internetCafes.spms.web.customer.model.customer.cooperation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客户合作记录
 */
@RestController
@RequestMapping("/ent/customer/cooperation")
public class EntCustomerCooperationController extends BaseWebController {

    @Autowired
    private IEntCustomerCooperationBizService cooperationBizService;

    /**
     * 合作记录消耗金额列表
     */
    @GetMapping("/consume/log/list/{cooperationId}")
    public RR<List<CustomerConsumeRes>> consumeList(@PathVariable("cooperationId") Long cooperationId) {
        return RR.success(cooperationBizService.consumeList(cooperationId));
    }

    /**
     * 批量保存合作记录信息
     */
    @PostMapping("/batch/save")
    public RR<ExcelImportResultRes> batchSave(@RequestBody CooperationSaveBatchReq req) {
        return RR.success(cooperationBizService.batchSave(req));
    }

    /**
     * 导入excel文件读取数据
     */
    @PostMapping("/import")
    public RR<List<CooperationImportExcel>> importExcel(MultipartFile file) {
        return RR.success(cooperationBizService.cooperationImportExcel(file));
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(@RequestBody CooperationExcelReq req, HttpServletResponse response) {
        List<CooperationExcel> cooperationExcels = cooperationBizService.cooperationExcel(req);
        ExcelUtils.exportExcel(cooperationExcels, "合作记录", "合作记录", CooperationExcel.class, "合作记录.xls", response);
    }

    /**
     * 合作记录分页
     */
    @PostMapping("/paging")
    public RR<PageItemVO<CustomerCooperationPagingRes>> paging(@RequestBody PageItemDTO<CustomerCooperationPagingReq> req) {
        return RR.success(cooperationBizService.paging(req));
    }

    /**
     * 删除合作记录
     */
    @SysLog("删除合作记录")
    @DeleteMapping("/{idList}")
    public RR<Void> delete(@PathVariable List<Long> idList) {
        return cooperationBizService.deleteByIdList(idList) ? RR.success() : RR.fail();
    }

    /**
     * 修改客户合作记录
     */
    @SysLog("修改客户合作记录")
    @PutMapping()
    public RR<Void> update(@RequestBody CustomerCooperationUpdateReq req) {
        return cooperationBizService.update(req) ? RR.success() : RR.fail();
    }

    /**
     * 保存
     */
    @SysLog("新增客户合作记录")
    @PostMapping("/save")
    public RR<Void> save(@RequestBody CustomerCooperationSaveReq req) {
        return cooperationBizService.save(req) ? RR.success() : RR.fail();
    }
}
